from abc import ABC
from sqlalchemy.sql.elements import TextClause
from sqlalchemy.sql.selectable import Select
from sqlalchemy.sql.dml import Update, Delete
from sqlalchemy.ext.asyncio.session import AsyncSession
from sqlmodel import SQLModel


from ...init.Global import Session


class BaseMapper(ABC):
    session: AsyncSession = None

    def __call__(self, session: Session):
        self.session = session
        return self

    async def insert_by_entity(self, entity: SQLModel):
        self.session.add(entity)

    async def insert_batch_by_entity(self, entitys: list[SQLModel]):
        self.session.add_all(entitys)

    async def execute_by_sql(self, sql: TextClause):
        return await self.session.execute(sql)

    async def select(self, sql: Select):
        return (await self.session.execute(sql)).scalars().all()

    async def update(self, sql: Update):
        await self.session.execute(sql)

    async def delete(self, sql: Delete):
        await self.session.execute(sql)
